package org.ns.tomdog.search;

public class FuzzySearch {
    SearchNode head = new SearchNode(' ', null);
    
    public void add(String text, Object value) {
        SearchNode node = head;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            node = node.getOrCreate(c, node);
        }
        node.setValue(value);
    }

    /**
     * 搜索前缀
     * @param text
     * @return
     */
    public Object searchPrefix(String text) {
        SearchNode node = head;
        SearchNode mark = null;
        SearchNode end = null;

        int markIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            SearchNode next = node.getNext(c);
            if (next == null) {
                if (mark != null) {
                    i = markIndex;
                    next = mark;
                    mark = null;
                } else {
                    break;
                }
            } else {
                if (next.isWildcard()) {
                    SearchNode afterNext = next.getNext(c);
                    mark = next;
                    markIndex = i;
                    if (afterNext != null) {
                        next = afterNext;
                        markIndex = i + 1;
                    }
                }
                if (next.getValue() != null) {
                    end = next;
                }
            }

            node = next;
        }
        return end == null ? null : end.getValue();
    }

    /**
     * 完整匹配
     * @param text
     * @return
     */
    public Object search(String text) {
        SearchNode node = head;
        SearchNode mark = null;
        int markIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            node = node.getNext(c);
            if (node == null) {
                if (mark != null) {
                    node = mark;
                    i = markIndex;
                    mark = null;
                } else {
                    break;
                }

            } else {
                if (node.isWildcard()) { // 如果是通配符
                    SearchNode next = node.getNext(c);
                    mark = node;
                    markIndex = i;
                    if (next != null) {
                        node = next;
                        markIndex = i + 1;
                    }
                }
            }

        }
        return node == null ? null : node.getValue();
    }
    public static void main(String[] args){
      FuzzySearch fuzzySearch = new FuzzySearch();
      fuzzySearch.add("/", "-----");
      fuzzySearch.add("/adm", "-----1");
      fuzzySearch.add("33*22", "-----2");
      fuzzySearch.add("33*33*123.jsp", "-----3");
      fuzzySearch.add("33*33*123", "-----4");

        System.out.println(fuzzySearch.searchPrefix("/123"));
        System.out.println(fuzzySearch.searchPrefix("/adm123"));
        System.out.println(fuzzySearch.searchPrefix("/adx123"));
        System.out.println(fuzzySearch.search("331133123.jsp"));
        System.out.println(fuzzySearch.search("12344"));
    }

}
